<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
    <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="120px"
      style="width: 90%; margin-left: 50px" :disabled="dialogStatus === 'look'">
      <el-row :gutter="40">
        <el-col :span="12">
          <el-form-item label="任务名称" prop="jobName">
            <el-input v-model="temp.jobName" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="任务状态" prop="checkStatus">
            <el-radio v-for="(item, index) in dictList('is_use')" :key="index" v-model="temp.checkStatus"
              :disabled="textMap[dialogStatus] == '新建' && index == 0" :label="item.value">
              {{ item.label }}
            </el-radio>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="cron表达式" prop="cronExpression">
            <el-input v-model="temp.cronExpression" @focus="openCron" />
          </el-form-item>
        </el-col>
      </el-row>
      <div style="margin-bottom: 12px">
        <el-button class="filter-item" style="margin-left: 10px" type="primary" :disabled="dialogStatus == 'look'"
          icon="el-icon-edit" @click="handleAdd">
          {{ $t('table.add') }}
        </el-button>
      </div>
      <el-table ref="table" :key="tableKey" :data="list" border fit highlight-current-row style="width: 100%">
        <el-table-column label="核查数据源名称" min-width="150px">
          <template slot-scope="{ row }">
            <span>{{ row.sourceName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="核查表名称" min-width="150px">
          <template slot-scope="{ row }">
            <span>{{ row.tableName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="核查字段名称" min-width="150px">
          <template slot-scope="{ row }">
            <span>{{ row.columnName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="规则名称" min-width="150px">
          <template slot-scope="{ row }">
            <span>{{ row.ruleName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="规则值" min-width="80px">
          <template slot-scope="{ row }">
            <span>{{ row.ruleValue }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="center" min-width="150" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button type="text" size="small" icon="el-icon-delete" class="delete-text-btn"
              @click="handleDelete(scope.$index)">
              {{ $t('table.delete') }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">
        {{ $t('table.cancel') }}
      </el-button>
      <el-button type="primary" :loading="loading"
        @click="dialogStatus === 'create' ? createData() : dialogStatus === 'look' ? (dialogFormVisible = false) : updateData()">
        {{ $t('table.confirm') }}
      </el-button>
    </div>
    <addFrom ref="addFrom" @addList="addList" />
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogCron" :append-to-body="true"
      :close-on-click-modal="false">
      <Crontab @fill="getStr" @hide="dialogCron = false"></Crontab>
    </el-dialog>
  </el-dialog>
</template>

<script>
import { createmetadataJob, updatemetadataJob, getmetadataJob, getSourceList, queryTableList, queryColumnList, queryRules } from '@/api/job/metadataJob'
import { getTableList } from '@/api/masterdata/masterdataTable'
import Crontab from '@/components/Crontab/index.vue'
import addFrom from './child/addFrom.vue'
export default {
  name: 'metadataJobForm',
  components: { Crontab, addFrom },
  data() {
    return {
      sourceData: [],
      rules: {
        jobName: [{ required: true, message: '任务名称为必填项', trigger: 'blur' }],
        checkStatus: [{ required: true, message: '任务状态为必填项', trigger: 'blur' }],
        cronExpression: [{ required: true, message: 'cron表达式为必填项', trigger: 'blur' }],
      },
      temp: {},
      textMap: {
        update: '编辑',
        create: '新建',
        look: '查看',
      },
      tableKey: 0,
      examinelist: [],
      Checkfieldlist: [],
      loading: false,
      dialogFormVisible: false,
      dialogCron: false,
      dialogStatus: '',
      ruleslist: [],
      postid: 0,
      total: 0,
      list: [], //表数据
    }
  },

  methods: {
    setList(id) {
      getmetadataJob(id).then((response) => {
        if (response.data.code === 200) {
          this.temp = response.data.data
          this.list = response.data.data.qualityInspection || []
        } else {
          this.dialogFormVisible = false
          this.$message.error(response.data.msg)
        }
      })
    },
    //选择所属数据源
    choseBelongs(data) {
      this.temp.tableId = ''
      this.temp.tableName = ''
      this.temp.sourceName = data.sourceName
      this.temp.sourceId = data.id
      getTableList({ sourceId: data.id }).then((res) => {
        if (res.data.code === 200) {
          this.examinelist = res.data.data
        }
      })
    },
    //选择表名称
    choseTable(data) {
      this.temp.tableId = data.id
      this.temp.tableName = data.tableName
    },
    openCron() {
      this.dialogCron = true
    },
    //获取返回的cron表达式
    getStr(str) {
      this.temp.cronExpression = str
    },
    addList(data) {
      // console.log(data)
      this.list.push(data)
    },
    //添加
    handleAdd() {
      this.$refs.addFrom.handleUpdate(this.temp)
      
    },
    //删除列
    handleDelete(index) {
      this.list.splice(index, 1)
    },
    getList() {
      this.$emit('refreshList')
    },
    resetTemp() {
      this.loading = false
      this.temp = {
        id: undefined,
        jobName: undefined,
        checkStatus: undefined,
        cronExpression: undefined,
        sourceId: undefined,
        sourceName: undefined,
        tableId: undefined,
        tableName: undefined,
        qualityInspection: [],
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.resetTemp()
      this.list = []
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.temp.qualityInspection = this.list
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          createmetadataJob(this.temp).then((response) => {
            this.loading = false
            if (response.data.code === 0) {
              this.getList()
              this.dialogFormVisible = false
              this.$message.success(response.data.msg)
            } else {
              this.$message.error(response.data.msg)
            }
          })
        }
      })
    },
    handleLook(id) {
      this.id = id
      this.resetTemp()
      this.dialogStatus = 'look'
      this.dialogFormVisible = true
      this.setList(id)
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleUpdate(id) {
      this.id = id
      this.resetTemp()
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.setList(id)
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.temp.qualityInspection = this.list
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          console.log(this.temp)
          updatemetadataJob(this.temp, this.id).then((response) => {
            this.loading = false
            if (response.data.code === 0) {
              this.dialogFormVisible = false
              this.getList()
              this.$message.success(response.data.msg)
            } else {
              this.$message.error(response.data.msg)
            }
          })
        }
      })
    },
  },
}
</script>
<style lang="scss" scoped>
::v-deep(::-webkit-scrollbar) {
  display: block !important;
}
</style>
